package com.web.exceptions;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CustomExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//        //统一异常拦截
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("error");
//        request.setAttribute("msg", "Error");
//        return modelAndView;
        //统一异常拦截
        if (ex instanceof UnauthorizedException){
            return new ModelAndView("forward:/unauthorized.jsp");
        }else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("error");
            modelAndView.addObject("errorMsg", "对不起，我错了！");
            return modelAndView;
        }
    }
}
